package com.mnt.washio.adapters;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;

import com.mnt.washio.R;
import com.mnt.washio.activities.AddressActivity;
import com.mnt.washio.objects.Address;
import com.mnt.washio.utils.network.WebServiceCommunicatorListener;
import com.mnt.washio.utils.network.ws.BaseWSControl;
import com.mnt.washio.utils.network.ws.BaseWSControl.WebServiceFlag;
import com.mnt.washio.utils.network.ws.DeleteAddressWSControl;
import com.mnt.washio.views.CustomFontTextView;

public class AddressListAdapter extends BaseAdapter implements
		WebServiceCommunicatorListener {

	private Context mContext;
	private ArrayList<Address> addresss;
	private OnItemAdressClickListener oIAC;

	public AddressListAdapter(Context mContext, ArrayList<Address> addresss) {
		this.mContext = mContext;
		this.addresss = addresss;
	}

	@Override
	public int getCount() {
		return addresss.size();
	}

	@Override
	public Address getItem(int position) {
		return addresss.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final Address ad = getItem(position);
		if (convertView == null) {
			LayoutInflater mInflater = (LayoutInflater) mContext
					.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(R.layout.item_list_payment, null);
		}

		CustomFontTextView txtTitle = (CustomFontTextView) convertView
				.findViewById(R.id.tv1);
		CustomFontTextView txtExpire = (CustomFontTextView) convertView
				.findViewById(R.id.tv2);

		txtTitle.setText(ad.getAddress());
		txtExpire.setText(ad.getName());

		ImageView imvDelete = (ImageView) convertView
				.findViewById(R.id.imv_payment_delete);
		imvDelete.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				new AlertDialog.Builder(mContext)
						.setTitle("Delete Address")
						.setMessage("Are you sure you want to delete this address?")
						.setPositiveButton("Ok",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub
										new DeleteAddressWSControl(mContext,
												AddressListAdapter.this, ad
														.getObjectId())
												.execute();
									}
								})
						.setNegativeButton("Cancel",
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub

									}
								}).show();
			}
		});
		
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(oIAC != null) oIAC.onItemClick(addresss.get(position));
			}
		});

		return convertView;
	}

	@Override
	public void onConnectionOpen(BaseWSControl wsControl, WebServiceFlag flag) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onConnectionError(BaseWSControl wsControl, WebServiceFlag flag,
			String error) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onConnectionDone(BaseWSControl wsControl, WebServiceFlag flag,
			Object result) {
		// TODO Auto-generated method stub
		if (flag == WebServiceFlag.REMOVE_ADDRESS) {
			((AddressActivity) mContext).loadData();
			Toast.makeText(mContext, "Delete successfully!", Toast.LENGTH_SHORT)
					.show();
		}
	}

	@Override
	public void onUpdateData(BaseWSControl wsControl, WebServiceFlag flag,
			Object data) {
		// TODO Auto-generated method stub

	}
	
	public void setOnItemAdressClickListener(OnItemAdressClickListener oIAC){
		this.oIAC = oIAC;
	}
	
	public interface OnItemAdressClickListener{
		public void onItemClick(Address add);
	}
}
